package com.zhubjiept.app.jinrirong.activity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhubjiept.app.R;
import com.zhubjiept.app.common.base.BaseMvpActivity;
import com.zhubjiept.app.common.widget.TeamMember;
import com.zhubjiept.app.common.widget.TeamMemberAdapter;
import com.zhubjiept.app.jinrirong.config.UserManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangsy on 2019/1/20.
 */

public class MyTeamActivity extends BaseMvpActivity<TeamMemberView, TeamMemberRequestPresenter> implements TeamMemberView {

    @BindView(R.id.team_member)
    ListView mRvHelpList;
    List<TeamMember> members = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.rg_top)
    RadioGroup mRadioGroup;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.layout_my_team;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRadioGroup.check(R.id.btn_team_under);
        //默认展示直属团队
        new Thread(() -> {
            mPresenter.requestMyTeamInfo(UserManager.getInstance().getToken(), 1);
        }).start();
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.btn_team_under:
                        new Thread(() -> {
                            mPresenter.requestMyTeamInfo(UserManager.getInstance().getToken(), 1);
                        }).start();
                        break;
                    case R.id.btn_team_sub:
                        new Thread(() -> {
                            mPresenter.requestMyTeamInfo(UserManager.getInstance().getToken(), 2);
                        }).start();
                        break;
                }
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected TeamMemberRequestPresenter createPresenter() {
        return new TeamMemberRequestPresenter();
    }

    @Override
    public void showLoadingView() {
        super.showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }


    @Override
    public void onTokenInvalidate() {

    }

    @Override
    public void onNetworkConnectFailed() {

    }

    @Override
    public void onRemoteSucceed(JSONArray members) {
        this.members = new ArrayList<>();
        if (members != null && members.size() > 0) {
            for (Object obj : members) {
                this.members.add(((JSONObject) obj).toJavaObject(TeamMember.class));
            }
        }
        mRvHelpList.setAdapter(new TeamMemberAdapter(this, this.members));
    }

    @Override
    public void onRemoteFailed(String msg) {

    }
}